/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package suit.ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

/**
 * A data entry field allowing the user to input a single line of text.
 * 
 * Currently, it creates a TextBox from the LCDUI package to allow the user the
 * input text (just like Opera Mini 4). In the future however it will be more
 * like Opera Mini 5 and use an on-screen keyboard.
 * 
 * @author Devin Samarin
 * 
 */
public class Entry extends Widget implements CommandListener {

	protected int focusable = FOCUS_TRUE;

	private String text;

	private int height = 30;

	/**
	 * Construct a new Entry, without any text
	 */
	public Entry() {
		this.text = "";
	}

	/**
	 * Construct a new Entry, with the specified text
	 * 
	 * @param text
	 *            the contents of the Entry
	 */
	public Entry(String text) {
		this.text = text;
	}

	/**
	 * Replace the current contents of the Entry with the supplied text.
	 * 
	 * @param text
	 *            the contents of the Entry
	 */
	public void setText(String text) {
		this.text = text;
		invalidate(this);
	}

	/**
	 * Get the text currently showing in the Entry. This is typically the most
	 * significant method as it is the one you use to get the result of the
	 * user's activity upon receiving a Entry.Activate signal. (not implemented)
	 * 
	 * @return the contents of the Entry
	 */
	public String getText() {
		return this.text;
	}

	protected boolean onExpose(Event e) {

		e.theme.drawEntry(e.graphics, this, allocation);
		e.theme.setEntryText(e.graphics);

		height = e.theme.getEntryTextFont().getHeight() + 20;

		e.graphics.drawString(this.text, allocation.x + 5, allocation.y
				+ allocation.h / 2 - Font.getDefaultFont().getHeight() / 2,
				Graphics.TOP | Graphics.LEFT);
		return false;
	}

	protected boolean onPointerPressed(Event e) {
		if (localizeEvent(e) != null) {
			activate();
		}
		return false;
	}

	public void activate() {
		touch();
		TextBox test = new TextBox("Input", this.text, 170, TextField.ANY);
		test.addCommand(new Command("OK", Command.OK, 1));
		test.addCommand(new Command("Cancel", Command.CANCEL, 2));
		test.setCommandListener(this);
		screen.app.setScreen(test);
	}

	Rectangle getRequisition() {
		return new Rectangle(0, 0,
				Font.getDefaultFont().stringWidth(this.text), height);
	}

	protected Changed changeHandler;

	public void connect(Changed handler) {
		changeHandler = handler;
	}

	public interface Changed {
		public void onChanged(Entry source);
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.OK) {
			setText(((TextBox) d).getString());

			if (changeHandler != null)
				changeHandler.onChanged(this);
		}
		screen.app.setScreen(screen);

	}

}
